package com.zz.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.zz.entity.Deliverylist;
import com.zz.entity.DeliverylistExcel;
import com.zz.entity.Product;
import com.zz.repository.DTODao;
import com.zz.repository.DeliverylistRepository;
import com.zz.repository.ProductRepository;
import com.zz.repository.SupplierRepository;
import com.zz.repository.WareRepository;
@Service
public class DeliverylistService {
	@Resource
	WareRepository wareRepository;
	@Resource
	SupplierRepository supplierRepository;
	@Resource
	ProductRepository productRepository;
	@Resource
	DeliverylistRepository deliverylistRepository;
	@Resource
	DTODao dtodao;
	
	//保存--更新产品信息
	public Product saveproduct(Product p) {
		return productRepository.save(p);
	}
	//保存入库单信息
	public Deliverylist savedeliverylist(Deliverylist deliverylist) {
		return deliverylistRepository.save(deliverylist);
	}
	//显示所有入库单并进行分页
	public Page<Object[]> showAllDeliverylist(String startpage,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(startpage), Integer.parseInt(limit));
		return deliverylistRepository.showAllDeliverylist(pageable);
	}
	public ArrayList<DeliverylistExcel> showAllDeliverylist2(){
		ArrayList<DeliverylistExcel> ls=new ArrayList<DeliverylistExcel>();
		List<Map<String, Object>> listmap=dtodao.getlistexcel();
		for(int i=0;i<listmap.size();i++) {
			String delid=(String) listmap.get(i).get("id");
			String productname=(String) listmap.get(i).get("productname");
			String out_num= listmap.get(i).get("out_num").toString();
			String out_time=listmap.get(i).get("out_time").toString();
			String gysname=(String) listmap.get(i).get("gysname");
			String ckname=(String) listmap.get(i).get("ckname");
			String notes=(String) listmap.get(i).get("notes");
			SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			DeliverylistExcel deliverylistExcel=new DeliverylistExcel();
			java.util.Date outtime;
			try {
				outtime = s.parse(out_time);
				deliverylistExcel.setDelOut_time(outtime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			deliverylistExcel.setDelId(delid);
			deliverylistExcel.setProductname(productname);
			deliverylistExcel.setDelOut_num(Integer.parseInt(out_num));
			deliverylistExcel.setCkname(ckname);
			deliverylistExcel.setGysname(gysname);
			deliverylistExcel.setDelNotes(notes);
			ls.add(deliverylistExcel);
			}
		return ls;
	}
	
	//最近一周
	public Page<Object[]> showDeliverylistBy7day(String startpage,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(startpage), Integer.parseInt(limit));
		return deliverylistRepository.showDeliverylistBy7day(pageable);
	}
	//最近一个月
	public Page<Object[]> showDeliverylistBy30day(String startpage,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(startpage), Integer.parseInt(limit));
		return deliverylistRepository.showDeliverylistBy30day(pageable);
	}
	//一段时间查询
	public Page<Object[]> showDeliverylistBytime(String starttime,String lasttime,String startpage,String limit){
		Pageable pageable=PageRequest.of(Integer.parseInt(startpage), Integer.parseInt(limit));
		return deliverylistRepository.showDeliverylistBytime(starttime, lasttime, pageable);
	}
}
